package com.crm.business.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.crm.business.admin.service.AdminMessageService;
import com.crm.business.oa.mapper.OaEventMapper;
import com.crm.business.oa.service.*;
import com.crm.common.ka.core.feign.crm.entity.ExamineField;
import com.crm.common.ka.core.feign.examine.entity.ExamineConditionDataBO;
import com.crm.common.oa.entity.BO.QueryEventListBO;
import com.crm.common.oa.entity.PO.OaEvent;
import com.crm.common.oa.entity.PO.OaExamineField;
import com.crm.common.oa.entity.VO.OaExamineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class OaCallServiceImpl implements OaService {

    @Autowired
    private IOaEventService oaEventService;
    @Autowired
    private OaEventMapper eventMapper;
    @Autowired
    private IOaExamineService oaExamineService;
    @Autowired
    private IOaCommonService oaCommonService;
    @Autowired
    private IOaExamineFieldService examineFieldService;
    @Resource
    private AdminMessageService adminMessageService;


    public void eventNoticeCron(){
        DateTime nowDate = DateUtil.date();
        long startTime = DateUtil.beginOfDay(nowDate).getTime();
        long endTime = DateUtil.endOfDay(nowDate).getTime();
        QueryEventListBO queryEventListBO = new QueryEventListBO();
        queryEventListBO.setStartTime(startTime);
        queryEventListBO.setEndTime(endTime);
        List<OaEvent> oaEventList = eventMapper.queryList(queryEventListBO);
        oaEventService.eventNotice(oaEventList);
    }

    public List<OaExamineVO> transfer(List<OaExamineVO> recordList){
        List<OaExamineVO> transfer = oaExamineService.transfer(recordList);
        return transfer;
    }

    public Boolean initOaData(){
        return oaCommonService.initOaData();
    }

    public Boolean initCalendarData(){
        return oaCommonService.initCalendarData();
    }

    public Boolean initOaExamineData(){
        return oaCommonService.initOaExamineData();
    }

    public List<ExamineField> queryExamineField(Integer categoryId){
        List<OaExamineField> oaExamineFields = examineFieldService.queryField(categoryId);
        List<ExamineField> examineFields = new ArrayList<>();
        oaExamineFields.forEach(oaExamineField -> {
            boolean isNeed = Objects.equals(oaExamineField.getIsNull(),1) && ListUtil.toList(3,5,6,9).contains(oaExamineField.getType());
            if (isNeed){
                examineFields.add(BeanUtil.copyProperties(oaExamineField, ExamineField.class));
            }
        });
        return examineFields;
    }

    public Map<String, Object> getDataMapForNewExamine(ExamineConditionDataBO examineConditionDataBO){
        return oaExamineService.getDataMapForNewExamine(examineConditionDataBO);
    }

    public Boolean updateCheckStatusByNewExamine(ExamineConditionDataBO examineConditionDataBO){
        return oaExamineService.updateCheckStatusByNewExamine(examineConditionDataBO);
    }

    public OaExamineVO getOaExamineById(Integer oaExamineId){
        OaExamineVO examineVO = oaExamineService.getOaExamineById(oaExamineId);
        return examineVO;
    }

    public void saveDefaultField(Long categoryId){
        examineFieldService.saveDefaultField(categoryId);
    }

    public Boolean updateFieldCategoryId(Long newCategoryId, Long oldCategoryId){
        return examineFieldService.updateFieldCategoryId(newCategoryId,oldCategoryId);
    }
}
